3. Spring 整合 Mybatis、JUnit

您所在的位置:网站首页 spring boot整合mybatis 3. Spring 整合 Mybatis、JUnit

3. Spring 整合 Mybatis、JUnit

#3. Spring 整合 Mybatis、JUnit| 来源: 网络整理| 查看: 265

文章目录 1. Spring 整合 Mybatis1.1 Mybatis 基本使用回顾1.2 Spring 整合 Mybatis 2. Spring 整合 JUnit

1. Spring 整合 Mybatis 1.1 Mybatis 基本使用回顾

首先创建好数据库表:

-- 删除tbl_account表 drop table if exists tbl_account; -- 创建tbl_account表 create table tbl_account ( id int primary key auto_increment, name varchar(20), money double ); -- 添加数据 insert into tbl_account (name, money) values ('Tom', 1000), ('Jerry', 500); SELECT * FROM tbl_account;

在这里插入图片描述

然后编写代码,模块整体结构如下:

在这里插入图片描述

(1) pom.xml:添加相关依赖

org.mybatis mybatis 3.5.6 mysql mysql-connector-java 5.1.47

(2) jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false jdbc.username=root jdbc.password=123456

(3) sqlMapConfig.xml:mybatis 核心配置文件

DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

(4) 创建实体类

public class Account { private Integer id; private String name; private Double money; //getter、setter方法 //toString方法 }

(5) 接口方法与 SQL 注解

public interface AccountDao { @Insert("insert into tbl_account(name,money) values(#{name},#{money})") void save(Account account); @Delete("delete from tbl_account where id = #{id}") void delete(Integer id); @Update("update tbl_account set name = #{name}, money = #{money} where id = #{id}") void update(Account account); @Select("select * from tbl_account") List findAll(); @Select("select * from tbl_account where id = #{id}") Account findById(Integer id); }

(6) 测试

public class App { public static void main(String[] args) throws IOException { // 1.创建SqlSessionFactoryBuilder对象 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); // 2.加载SqlMapConfig.xml配置文件 InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml"); // 3.创建SqlSessionFactory引象 SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); // 4.获取SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); // 5.获取AccountDao接口的代理对象 AccountDao accountDao = sqlSession.getMapper(AccountDao.class); // 6.执行方法 Account ac = accountDao.findById(2); System.out.println(ac); //释放资源 sqlSession.close(); } } 1.2 Spring 整合 Mybatis

在这里插入图片描述

(1) 配置依赖:pom.xml

org.springframework spring-context 5.2.10.RELEASE com.alibaba druid 1.1.16 org.mybatis mybatis 3.5.6 mysql mysql-connector-java 5.1.47 org.springframework spring-jdbc 5.2.10.RELEASE org.mybatis mybatis-spring 1.3.0

(2) 数据库连接相关

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false jdbc.username=root jdbc.password=123456

JdbcConfig.class

public class JdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String userName; @Value("${jdbc.password}") private String password; @Bean//把方法的返回值定义成一个bean public DataSource dataSource(){ DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); return ds; } }

(3) 实体类

public class Account { private Integer id; private String name; private Double money; //getter、setter方法 //toString方法 }

(4) Mapper 接口

public interface AccountDao { @Insert("insert into tbl_account(name,money) values(#{name},#{money})") void save(Account account); @Delete("delete from tbl_account where id = #{id}") void delete(Integer id); @Update("update tbl_account set name = #{name}, money = #{money} where id = #{id}") void update(Account account); @Select("select * from tbl_account") List findAll(); @Select("select * from tbl_account where id = #{id}") Account findById(Integer id); }

(5) 业务层

public interface AccountService { void save(Account account); void delete(Integer id); void update(Account account); List findAll(); Account findById(Integer id); } @Service public class AccountServiceImpl implements AccountService { @Autowired//按类型注入 private AccountDao accountDao; public void save(Account account) { accountDao.save(account); } public void update(Account account){ accountDao.update(account); } public void delete(Integer id) { accountDao.delete(id); } public Account findById(Integer id) { return accountDao.findById(id); } public List findAll() { return accountDao.findAll(); } }

(6) Mybatis 配置类

mybatis 核心配置文件 ➡ mybatis 配置类:

在这里插入图片描述 在这里插入图片描述

//以后只改路径就行 public class MybatisConfig { //为第三方bean注入引用类型:为 bean 定义的方法设置形参即可,容器会根据类型自动装配对象 @Bean//把方法的返回值定义成一个bean public SqlSessionFactoryBean sqlSessionFactory(DataSource datasource) { SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); //给实体类起别名 ssfb.setTypeAliasesPackage("com.itheima.domain"); ssfb.setDataSource(datasource); return ssfb; } @Bean//把方法的返回值定义成一个bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); //自动代理,加载sql msc.setBasePackage("com.itheima.dao"); return msc; } }

(7) Spring 配置类

@Configuration//该类是配置类 @ComponentScan("com.itheima")//扫描这个包下的类,找bean @PropertySource("jdbc.properties") @Import({JdbcConfig.class,MybatisConfig.class})//引入这些类中的bean public class SpringConfig { }

(8) 测试

public class App2 { public static void main(String[] args) { //加载配置类 ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); //按类型获取bean AccountService accountService = ctx.getBean(AccountService.class); //执行方法 Account byId = accountService.findById(1); System.out.println(byId); } }

输出结果:

Account{id=1, name='Tom', money=1000.0} 2. Spring 整合 JUnit

在上节代码的基础上整合:

在这里插入图片描述

(1) 添加依赖

junit junit 4.12 test org.springframework spring-test 5.2.10.RELEASE

(2) 测试类

public class AccountServiceTest { @Autowired private AccountService accountService; @Test public void testFindById() { System.out.println(accountService.findById(1));; } @Test public void testFindAll(){ System.out.println(accountService.findAll()); } //其他的测试方法同理 }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3